package com.maixin.code.dao;

import java.util.List;

import org.hibernate.Query;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.maixin.code.entity.UserLogin;
/**
 * 
 * @author shujie
 *
 * @param <T> 普通POJO对象
 */
public class BaseDAO<T> extends HibernateDaoSupport implements DAO<T> {
	private static final Logger log = LoggerFactory.getLogger(BaseDAO.class);
	public T create(T t) {
		try {
			getSession().save(t);
			return t;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public Integer findTabCnt(Class<T> t){
		try {
			String hql = " select count(*) from " + t.getName() + " ";
			Query query = getSession().createQuery(hql);
			return Integer.parseInt(query.list().get(0)+"");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public T delete(T t) {
		try {
			getSession().delete(t);

			return t;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public List<T> findByPropertes(String[] propertes, Object[] values, Class<T> t)
			throws Exception {
		try {
			if (((null != propertes) && (null != values) && (propertes.length != values.length))
					|| ((null != propertes) && (null == values))
					|| ((null == propertes) && (null != values))) {
				throw new Exception(" propertes or values is invalid !");
			}
			String hql = " from " + t.getName() + " ";
			if ((null != propertes) && (propertes.length > 0)) {
				for (int index = 0; index < propertes.length; index++) {
					if (index == 0) {
						hql += (" where " + propertes[index] + "=:" + propertes[index]);
					} else {
						hql += (" and " + propertes[index] + "=:" + propertes[index]);
					}
				}
			}

			Query query = getSession().createQuery(hql);
			if ((null != values) && (values.length > 0)) {
				for (int index = 0; index < values.length; index++) {
					query.setParameter(propertes[index], values[index]);
				}
			}
			List<T> list = (List<T>)query.list();

			return list;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public List<T> findAll(Class<T> t,Integer start,Integer limit) {
		try {
			String hql = " from " + t.getName() + " t order by t.id desc ";
			Query query = getSession().createQuery(hql).
					setFirstResult(start).
				      setMaxResults(limit);
			List<T> list = query.list();
			return list;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	

	public T save(T t) {
		try {
			getSession().save(t);
			return t;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public T update(T t) {
		try {
			getSession().update(t);
			return t;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public List<T> findAll(Class<T> t) {
		try {
			String hql = " from " + t.getName() + " t order by t.id desc ";
			Query query = getSession().createQuery(hql);
			List<T> list = query.list();
			return list;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public List<T> findByProperty(String property, Object value, Class<T> t)
			throws Exception {
		try {
			if ((null == property) && (null == value)) {
				throw new Exception(" property or value is invalid !");
			}
			String hql = " from " + t.getName() + " where " + property + "=:" + property;

			Query query = getSession().createQuery(hql);
			query.setParameter(property, value);
			List<T> list = (List<T>)query.list();

			return list;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	/**
	 * 
	 * @param propertyNames
	 * @param values
	 * @return
	 */
	public List<T> findByProperties(String[] propertyNames, Object[] values,Class<T> t){
		try {
			String queryString = "from " + t.getName() + " as model where ";
			for (int i = 0; i < propertyNames.length; i++) {
				queryString+= " model." +propertyNames[i]+ " = ?";
				if(i<propertyNames.length-1){
					queryString+= " and ";
				}
			}
					//+ propertyName + "= ?";
			Query queryObject = getSession().createQuery(queryString);
			for (int i = 0; i < values.length; i++) {
				queryObject.setParameter(i, values[i]);
			}
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}
	

}
